# -*- coding: utf-8 -*-

from common.models.department import Department
from common.models.user_tag import UserTag
from dingtalk import SecretClient
from flask import Blueprint, request, make_response, redirect
from common.models.user import User
import json, requests
from config.dingtalk_setting import AppKey,AppSecret,AgentId,role_hr


reserve_page = Blueprint("reserve_page", __name__)

##连接
client = SecretClient(AppKey, AppSecret)  # 新 access_token 获取方式

@reserve_page.route('/tagsearch', methods=['GET', 'POST'])
def tagsearch():
    tag = request.json.get('tag')
    department=request.json.get('department')

    if len(department)==0:
        departmentlist=Department.query.all()
        for i in departmentlist:
            department.append(i.id)
    print(department)
    if len(tag)==0 :
        taglist = UserTag.query.all()
        for i in taglist :
            tag.append(i.name)
    print(tag)


    member = User.query.filter((User.status == '0') | (User.status == '4')).filter(User.tag.in_(tag)).filter(User.depid.in_(department)).all()
    rts = []
    for item in member:
        temp = dict(id=item.id, name=item.name, age=item.age,
                    email=item.email, sex=item.sex, mobile=item.mobile,tag=item.tagname.name,department=item.department.name)
        rts.append(temp)
    return json.dumps(rts, ensure_ascii=False)


